/****************************************************************************************************
* Title: Workhorses of Opportunity, Howard and Weinstein
*
* Inputs:
*   - justnormasylum.dta
*   - county_outcomes_dta.dta
*   - online_table4-2-1.dta
*
* Outputs:
*   - causal_college_income_and_married.tex
*   - causal_income.tex
*   - causal_income_appendix.tex
*   - causal_college.tex
*   - causal_college_appendix.tex
*   - causal_married.tex
*   - causal_married_appendix.tex
*
* Description:
*   Estimate the effect of being in a "normal" school county on observed and causal outcomes at the
*   25th percentile for income, college, and marriage. Both unweighted and precision-weighted
*   regressions are run, with tables produced for paper and appendix.
****************************************************************************************************/

clear all
set maxvar 15000

* Prepare and merge normal school indicator
clear 
use "justnormasylum"
rename hasnormalschool hasnormal
keep hasnormal cty_fips 
tempfile normal
save `normal'

* Merge with county outcome data
clear
use county_outcomes_dta
gen cty_fips = state*1000 + county
rename state statefips
rename county countychetty
merge 1:m cty_fips using `normal', keep(2 3) nogen
save `normal', replace

* Merge in observational and causal estimates
clear
use online_table4-2-1.dta
gen cty_fips = cty1990
drop county_name cz cz_name cz_pop2000 statename state_id stateabbrv
merge 1:m cty_fips using `normal', keep(2 3) nogen

*******************************************
* SECTION: Income Outcome
*******************************************
local causalname = "kr26"
local obsname = "kfr"
gen `obsname'_pooled_pooled_p25__t100 = `obsname'_pooled_pooled_p25 * 100

reghdfe `obsname'_pooled_pooled_p25__t100 hasnormal, absorb(state) cluster(state) nocon
eststo observ_income

gen precision_weight_`causalname' = 1 / causal_p25_cz_cty_`causalname'_se^2

reghdfe `obsname'_pooled_pooled_p25__t100 hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo observ_income_weighted

reghdfe causal_p25_cz_cty_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo causal_income_unweighted

reghdfe causal_p25_cz_cty_`causalname' hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo causal_income

reghdfe perm_res_p25_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo perm_res_income

*******************************************
* SECTION: College Outcome
*******************************************
local causalname = "c1823"
local obsname = "somecoll"
gen `obsname'_pooled_pooled_p25__t100 = `obsname'_pooled_pooled_p25 * 100

reghdfe `obsname'_pooled_pooled_p25__t100 hasnormal, absorb(state) cluster(state) nocon
eststo observ_college

gen precision_weight_`causalname' = 1 / causal_p25_cz_cty_`causalname'_se^2

reghdfe `obsname'_pooled_pooled_p25__t100 hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo observ_college_weighted

reghdfe causal_p25_cz_cty_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo causal_college_unweighted

reghdfe causal_p25_cz_cty_`causalname' hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo causal_college

reghdfe perm_res_p25_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo perm_res_college

*******************************************
* SECTION: Marriage Outcome
*******************************************
local causalname = "km26"
local obsname = "married"
gen `obsname'_pooled_pooled_p25__t100 = `obsname'_pooled_pooled_p25 * 100

reghdfe `obsname'_pooled_pooled_p25__t100 hasnormal, absorb(state) cluster(state) nocon
eststo observ_married

gen precision_weight_`causalname' = 1 / causal_p25_cz_cty_`causalname'_se^2

reghdfe `obsname'_pooled_pooled_p25__t100 hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo observ_married_weighted

reghdfe causal_p25_cz_cty_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo causal_married_unweighted

reghdfe causal_p25_cz_cty_`causalname' hasnormal [aw=precision_weight_`causalname'], absorb(state) cluster(state) nocon
eststo causal_married

reghdfe perm_res_p25_`causalname' hasnormal, absorb(state) cluster(state) nocon
eststo perm_res_married

*******************************************
* SECTION: Add Metadata for esttab Tables
*******************************************



estadd local weighted="Unweighted" : observ_married observ_college observ_income causal_married_unweighted causal_college_unweighted causal_income_unweighted perm_res_college perm_res_married perm_res_income
estadd local interpretation="Effect on Place": observ_married observ_college observ_income observ_college_weighted observ_married_weighted observ_income_weighted perm_res_married perm_res_college perm_res_income 
estadd local scale="Per Childhood": observ_married observ_college observ_income perm_res_college perm_res_married perm_res_income observ_married_weighted observ_college_weighted observ_income_weighted


estadd local weighted="Precision Weights": causal_married causal_college causal_income observ_married_weighted observ_college_weighted observ_income_weighted
estadd local interpretation="Effect on Person": causal_married causal_college causal_income causal_married_unweighted causal_college_unweighted causal_income_unweighted
estadd local scale="Per Year": causal_married causal_college causal_income causal_married_unweighted causal_college_unweighted causal_income_unweighted


*******************************************
* SECTION: Variable Labels for Display
*******************************************
label var hasnormal "Normal"
label var kfr_pooled_pooled_p25__t100 "\shortstack{Family Income  \\ Percentile, 2014-15}"
label var somecoll_pooled_pooled_p25__t100 "\shortstack{Some College \\ Age 25+}"
label var married_pooled_pooled_p25__t100 "\shortstack{Married \\ 2015}"

label var causal_p25_cz_cty_kr26 "\shortstack{Family Income \\  Percentile, Age 26}"
label var causal_p25_cz_cty_c1823 "\shortstack{Attended College \\ Age 18-23}"
label var causal_p25_cz_cty_km26 "\shortstack{Married \\ Age 26}"

label var perm_res_p25_kr26 "\shortstack{Family Income \\  Percentile, Age 26}"
label var perm_res_p25_c1823 "\shortstack{Attended College \\ Age 18-23}"
label var perm_res_p25_km26 "\shortstack{Married \\ Age 26}"

*******************************************
* SECTION: Output LaTeX Tables
*******************************************
esttab observ_college causal_college observ_income causal_income observ_married causal_married, ///
    se star(+ .1 * .05 ** .01 *** .001) label nonote ///
    scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_college causal_college observ_income causal_income observ_married causal_married using ///
    "causal_college_income_and_married.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

* Appendix tables
esttab observ_income observ_income_weighted perm_res_income causal_income_unweighted causal_income, ///
    se star(+ .1 * .05 ** .01 *** .001) label nonote replace ///
    scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_married observ_married_weighted perm_res_married causal_married_unweighted causal_married, ///
    se star(+ .1 * .05 ** .01 *** .001) label nonote replace ///
    scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_college observ_college_weighted perm_res_college causal_college_unweighted causal_college, ///
    se star(+ .1 * .05 ** .01 *** .001) label nonote replace ///
    scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

* Individual tables
esttab observ_income causal_income using "causal_income.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_income observ_income_weighted perm_res_income causal_income_unweighted causal_income using ///
    "causal_income_appendix.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_college causal_college using "causal_college.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_college observ_college_weighted perm_res_college causal_college_unweighted causal_college using ///
    "causal_college_appendix.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_married causal_married using "causal_married.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")

esttab observ_married observ_married_weighted perm_res_married causal_married_unweighted causal_married using ///
    "causal_married_appendix.tex", se star(+ .1 * .05 ** .01 *** .001) ///
    label nonote tex replace scalar("weighted Weights" "scale Scale" "interpretation Interpretation")
